﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sunboxsoft.ASMS.Client.Domain.Calculation
{
    public static class MaintanceBillCalculation
    {
        /// <summary>
        /// 计算金额
        /// </summary>
        /// <param name="serviceItemArray"></param>
        /// <param name="materialArray"></param>
        /// <param name="payItemArray"></param>
        /// <param name="multLineDetail">是否换行显示明细</param>
        /// <param name="detail"></param>
        /// <returns></returns>
        public static decimal Calc(WebRef.EMaintenanceBillServiceItem[] serviceItemArray,
            WebRef.EMaintenanceBillMaterial[] materialArray,
            WebRef.EMaintenanceBillPayItem[] payItemArray, bool multLineDetail, out string detail)
        {
            decimal amount = 0.0M;
            StringBuilder detailBuilder = new StringBuilder();

            decimal segmentOriginalAmount = 0.0M;
            decimal segmentAcutalAmount = 0.0M;
            if (serviceItemArray != null)
            {
                for (int index = 0; index < serviceItemArray.Length; index++)
                {
                    segmentOriginalAmount += serviceItemArray[index].Price * serviceItemArray[index].Hour;
                    segmentAcutalAmount += serviceItemArray[index].Price * serviceItemArray[index].Hour * serviceItemArray[index].Discount;
                }
                amount += segmentAcutalAmount;
                if (multLineDetail)
                {
                    detailBuilder.Append("服务项目:");
                    detailBuilder.AppendFormat("+{0:N2}({1}服务项目)", segmentAcutalAmount, serviceItemArray.Length);
                    detailBuilder.AppendLine();
                }
                else
                {
                    detailBuilder.AppendFormat("+{0:N2}({1}服务项目)", segmentAcutalAmount, serviceItemArray.Length);
                }
            }
            else
            {
                if (multLineDetail)
                {
                    detailBuilder.Append("服务项目:");
                    detailBuilder.AppendFormat("+{0:N2}(0服务项目)", segmentAcutalAmount);
                    detailBuilder.AppendLine();
                }
                else
                {
                    detailBuilder.AppendFormat("+{0:N2}(0服务项目)", segmentAcutalAmount);
                }
            }

            segmentOriginalAmount = 0.0M;
            segmentAcutalAmount = 0.0M;
            if (materialArray != null)
            {
                for (int index = 0; index < materialArray.Length; index++)
                {
                    segmentOriginalAmount += materialArray[index].Price * materialArray[index].Count;
                    segmentAcutalAmount += materialArray[index].Price * materialArray[index].Count * materialArray[index].Discount;
                }
                amount += segmentAcutalAmount;
                if (multLineDetail)
                {
                    detailBuilder.Append("商品物料:");
                    detailBuilder.AppendFormat("+{0:N2}({1}商品)", segmentAcutalAmount, materialArray.Length);
                    detailBuilder.AppendLine();
                }
                else
                {
                    detailBuilder.AppendFormat("+{0:N2}({1}商品)", segmentAcutalAmount, materialArray.Length);
                }
            }
            else
            {
                if (multLineDetail)
                {
                    detailBuilder.Append("商品物料:");
                    detailBuilder.AppendFormat("+{0:N2}(0商品)", segmentAcutalAmount);
                    detailBuilder.AppendLine();
                }
                else
                {
                    detailBuilder.AppendFormat("+{0:N2}(0商品)", segmentAcutalAmount);
                }
            }

            decimal discountAmount = 0.0M;
            int discountCount = 0;
            decimal paymentAmount = 0.0M;
            int paymentCount = 0;
            if (payItemArray != null)
            {
                for (int index = 0; index < payItemArray.Length; index++)
                {
                    if (payItemArray[index].PayItemType == Domain.Constant.PayItemTypes.Discount)
                    {
                        discountAmount += payItemArray[index].Amount;
                        discountCount++;
                    }
                    else
                    {
                        paymentAmount += payItemArray[index].Amount;
                        paymentCount++;
                    }
                }
                amount -= discountAmount;
                amount += paymentAmount;
                if (multLineDetail)
                {
                    detailBuilder.Append("其他项目:");
                    detailBuilder.AppendFormat("+{0:N2}({1}付费项)", paymentAmount, paymentCount);
                    detailBuilder.AppendFormat("-{0:N2}({1}优惠项)", discountAmount, discountCount);
                    detailBuilder.AppendLine();
                }
                else
                {
                    detailBuilder.AppendFormat("+{0:N2}({1}付费项)", paymentAmount, paymentCount);
                    detailBuilder.AppendFormat("-{0:N2}({1}优惠项)", discountAmount, discountCount);
                }
            }
            else
            {
                if (multLineDetail)
                {
                    detailBuilder.Append("其他项目:");
                    detailBuilder.AppendFormat("+{0:N2}(0付费项)", paymentAmount);
                    detailBuilder.AppendFormat("-{0:N2}(0优惠项)", discountAmount);
                    detailBuilder.AppendLine();
                }
                else
                {
                    detailBuilder.AppendFormat("+{0:N2}(0付费项)", paymentAmount);
                    detailBuilder.AppendFormat("-{0:N2}(0优惠项)", discountAmount);
                }
            }

            if (multLineDetail)
            {
                detailBuilder.AppendLine("----------------------------------------");
            }

            detail = detailBuilder.ToString();
            return amount;
        }


        /// <summary>
        /// 计算金额
        /// </summary>
        /// <param name="serviceItemArray"></param>
        /// <param name="materialArray"></param>
        /// <param name="payItemArray"></param>
        /// <param name="multLineDetail">是否换行显示明细</param>
        /// <param name="detail"></param>
        /// <returns></returns>
        public static decimal Calc(WebRef.VMaintenanceBillServiceItem[] serviceItemArray,
            WebRef.VMaintenanceBillMaterial[] materialArray,
            WebRef.VMaintenanceBillPayItem[] payItemArray, bool multLineDetail, out string detail)
        {
            decimal amount = 0.0M;
            StringBuilder detailBuilder = new StringBuilder();

            decimal segmentOriginalAmount = 0.0M;
            decimal segmentAcutalAmount = 0.0M;
            if (serviceItemArray != null)
            {
                for (int index = 0; index < serviceItemArray.Length; index++)
                {
                    segmentOriginalAmount += serviceItemArray[index].Price * serviceItemArray[index].Hour;
                    segmentAcutalAmount += serviceItemArray[index].Price * serviceItemArray[index].Hour * serviceItemArray[index].Discount;
                }
                amount += segmentAcutalAmount;
                if (multLineDetail)
                {
                    detailBuilder.Append("服务项目:");
                    detailBuilder.AppendFormat("+{0:N2}({1}服务项目)", segmentAcutalAmount, serviceItemArray.Length);
                    detailBuilder.AppendLine();
                }
                else
                {
                    detailBuilder.AppendFormat("+{0:N2}({1}服务项目)", segmentAcutalAmount, serviceItemArray.Length);
                }
            }
            else
            {
                if (multLineDetail)
                {
                    detailBuilder.Append("服务项目:");
                    detailBuilder.AppendFormat("+{0:N2}(0服务项目)", segmentAcutalAmount);
                    detailBuilder.AppendLine();
                }
                else
                {
                    detailBuilder.AppendFormat("+{0:N2}(0服务项目)", segmentAcutalAmount);
                }
            }

            segmentOriginalAmount = 0.0M;
            segmentAcutalAmount = 0.0M;
            if (materialArray != null)
            {
                for (int index = 0; index < materialArray.Length; index++)
                {
                    segmentOriginalAmount += materialArray[index].Price * materialArray[index].Count;
                    segmentAcutalAmount += materialArray[index].Price * materialArray[index].Count * materialArray[index].Discount;
                }
                amount += segmentAcutalAmount;
                if (multLineDetail)
                {
                    detailBuilder.Append("商品物料:");
                    detailBuilder.AppendFormat("+{0:N2}({1}商品)", segmentAcutalAmount, materialArray.Length);
                    detailBuilder.AppendLine();
                }
                else
                {
                    detailBuilder.AppendFormat("+{0:N2}({1}商品)", segmentAcutalAmount, materialArray.Length);
                }
            }
            else
            {
                if (multLineDetail)
                {
                    detailBuilder.Append("商品物料:");
                    detailBuilder.AppendFormat("+{0:N2}(0商品)", segmentAcutalAmount);
                    detailBuilder.AppendLine();
                }
                else
                {
                    detailBuilder.AppendFormat("+{0:N2}(0商品)", segmentAcutalAmount);
                }
            }

            decimal discountAmount = 0.0M;
            int discountCount = 0;
            decimal paymentAmount = 0.0M;
            int paymentCount = 0;
            if (payItemArray != null)
            {
                for (int index = 0; index < payItemArray.Length; index++)
                {
                    if (payItemArray[index].PayItemType == Domain.Constant.PayItemTypes.Discount)
                    {
                        discountAmount += payItemArray[index].Amount;
                        discountCount++;
                    }
                    else
                    {
                        paymentAmount += payItemArray[index].Amount;
                        paymentCount++;
                    }
                }
                amount -= discountAmount;
                amount += paymentAmount;
                if (multLineDetail)
                {
                    detailBuilder.Append("其他项目:");
                    detailBuilder.AppendFormat("+{0:N2}({1}付费项)", paymentAmount, paymentCount);
                    detailBuilder.AppendFormat("-{0:N2}({1}优惠项)", discountAmount, discountCount);
                    detailBuilder.AppendLine();
                }
                else
                {
                    detailBuilder.AppendFormat("+{0:N2}({1}付费项)", paymentAmount, paymentCount);
                    detailBuilder.AppendFormat("-{0:N2}({1}优惠项)", discountAmount, discountCount);
                }
            }
            else
            {
                if (multLineDetail)
                {
                    detailBuilder.Append("其他项目:");
                    detailBuilder.AppendFormat("+{0:N2}(0付费项)", paymentAmount);
                    detailBuilder.AppendFormat("-{0:N2}(0优惠项)", discountAmount);
                    detailBuilder.AppendLine();
                }
                else
                {
                    detailBuilder.AppendFormat("+{0:N2}(0付费项)", paymentAmount);
                    detailBuilder.AppendFormat("-{0:N2}(0优惠项)", discountAmount);
                }
            }

            if (multLineDetail)
            {
                detailBuilder.AppendLine("----------------------------------------");
            }

            detail = detailBuilder.ToString();
            return amount;
        }
    }
}
